package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.AlbumStat;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface AlbumStatMapper extends BaseMapper<AlbumStat> {


    @Select("select album_id,\n" +
            "       max(if(stat_type = 0401, stat_num, 0)) playStatNum,\n" +
            "       max(if(stat_type = 0402, stat_num, 0)) subscribeStatNum,\n" +
            "       max(if(stat_type = 0403, stat_num, 0)) buyStatNum,\n" +
            "       max(if(stat_type = 0404, stat_num, 0)) commentStatNum\n" +
            "from album_stat\n" +
            "where album_id = #{albumId} and is_deleted = 0\n" )
    AlbumStatVo selectAlbumStatVo(Long albumId);

    @Update("update album_stat\n" +
            "set stat_num = stat_num + #{count},\n" +
            "    update_time = now()\n" +
            "where album_id = #{albumId}\n" +
            "  and stat_type = #{statType}\n" +
            "  and is_deleted = 0\n")
    void updateAlbumStat(@Param("albumId") Long albumId, @Param("count") Integer count,@Param("statType") String statType);


}
